﻿using System.Reflection.Metadata;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using Microsoft.Office.Interop.Word;
using Microsoft.Win32;

namespace DeleteWordHeaderAndFooter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true; // 允许选择多个文件
            openFileDialog.Filter = "所有文件 (*.*)|*.*"; // 设置文件过滤器，根据需要调整
            var res = openFileDialog.ShowDialog();
            if (res.Value)
            {
                 txtDelWordPath.Dispatcher.Invoke(() =>
                {
                    txtDelWordPath.Text = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
                });
                lbResult.Items.Clear();
                foreach (string fileName in openFileDialog.FileNames)
                {
                    // 处理每个文件，例如显示文件名或其他操作
                    lbResult.Items.Add(System.IO.Path.GetFileName(fileName));
                }
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            var path = txtDelWordPath.Text.Trim();
            foreach (var item in lbResult.Items)
            {
                Microsoft.Office.Interop.Word.Application wordApp = null;
                Microsoft.Office.Interop.Word.Document doc = null;

                try
                {
                    // 创建Word应用程序实例
                    wordApp = new Microsoft.Office.Interop.Word.Application();
                    wordApp.Visible = false;

                    // 打开Word文档
                    doc = wordApp.Documents.Open($"{path}\\{item.ToString()}");
                    
                    // 遍历文档中的所有部分（通常对于一个文档来说只有一个部分）
                    foreach (Microsoft.Office.Interop.Word.Section section in doc.Sections)
                    {
                        // 删除每个部分的所有页眉
                        foreach (Microsoft.Office.Interop.Word.HeaderFooter header in section.Headers)
                        {
                            header.Range.Delete();
                        }

                        // 删除每个部分的所有页脚
                        foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in section.Footers)
                        {
                            footer.Range.Delete();
                        }
                    }

                    // 保存更改并关闭文档
                    doc.Save();
                    doc.Close();
                    wordApp.Quit();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("发生错误：" + ex.Message);
                }
                finally
                {
                    wordApp.Quit();
                    // 释放对象
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                }
            }
             
            MessageBox.Show("完成删除"); 
        }
    }
}